package example;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.equivalence.server.UpdateServer;

/**
 * updates a clients data directory
 * while ignoring any changes made to data/ignore.txt
 * @author gcconner
 *
 */
public class ExampleServer {
	private UpdateServer server;
	
	public static void main(String[] args) {
		ExampleServer exs = new ExampleServer();
		
		Thread thread = new Thread(exs.server);
		thread.start();
		
		try {
			char input = (char)System.in.read();
			
			//stop the server when the user presses q
			if (input=='q' && thread.isAlive()) {
				exs.server.stop();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ExampleServer() {
		server = new UpdateServer(1984);//run on port 1984
		
		server.addDirectory("data");
		
		server.addExcludeFile("data\\ignore.txt");
		
		//output when loggable events occur on the server, clients connecting etc..
		server.addServerEventListener(new EventListener());
		
		//keep track of the current status
		server.addStatusListener(new StatusListener());
		
		//initialise the server (has to be called when any changes are made to the 
		//configuration of the server i.e the methods above
		server.init();
	}
	
	private class EventListener implements ActionListener {

		/**
		 * the source object contains a string that indicates what event occured
		 */
		public void actionPerformed(ActionEvent e) {
			System.out.println((String)e.getSource());
			
		}
	}
	
	private class StatusListener implements ActionListener {
	
		/**
		 * the source object contains a string containing the status of the server
		 */
		public void actionPerformed(ActionEvent e) {
			System.out.println((String)e.getSource());
			
		}
	}
}
